package com.common;

import db_tool.Role;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;
import java.util.Map;

/**
 * Created by xiang on 2017/7/5.
 *
 */
public class RequestDataTool extends JdbcDaoSupport {

    //检查数据库中是否有申请中的数据，有三个状态：1. 未申请 2. 申请成功 3. 申请未批复 4. 申请被拒绝
    public String isRequesting(String username) {
        String status = "";
        int refusedNum = 0;
        List<Map<String, Object>> list = getJdbcTemplate().queryForList("SELECT * FROM Agree WHERE requestMoniter = ?", username);
        System.out.println("listSize" + list.size());
        for (Map<String, Object> map : list) {
            String groupAgree = (String) map.get("groupAgree");
            groupAgree = groupAgree.substring(0, 1);
            String moniterAgree = (String) map.get("moniterAgree");
            moniterAgree = moniterAgree.substring(0, 1);
            String same = (String) map.get("same");
            same = same.substring(0, 1);
            boolean flag = ("1".equals(groupAgree) && "1".equals(moniterAgree)) ||
                    ("1".equals(moniterAgree) && "1".equals(same));
            if (flag) {
                status = "agree";
            }
            if ("2".equals(groupAgree) || "2".equals(moniterAgree)) refusedNum++;
        }
        if (!status.equals("agree") && (list.size() > 0))
            status = "wait";
        if (refusedNum == list.size()) {
            status = "refuse";
        }
        if (list.size() == 0)
            status = "null";
        return status;
    }

    public String getSuperiorName(String name) {
        String sql = "SELECT superiorName FROM Account WHERE username = ?";
        List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql, name);
        return (String) list.get(0).get("superiorName");
    }

    public int insertRequest(String requestMoniter, String targetMoniter,
                             String same, String tableName, String requertGroup, String targetGroup) {
        String sql = "INSERT INTO Agree(requestMoniter, targetMoniter, groupAgree, moniterAgree, same" +
                ", tableName, requestGroup, targetGroup) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        return getJdbcTemplate().update(sql, requestMoniter, targetMoniter, "0", "0"
                , same, tableName, requertGroup, targetGroup);
    }

    public int updateMoniterSite(String institution, String invertigator, String invertigationDate, String site
            , String river, String photo, String startTime, String endTime, String startLongitude, String startLatitude
            , String endLongitude, String endLatitude, String weather, String temperature, String invertigtaionID) {
        String sql = "update MonitoringSite SET Institution=?, Investigator=?, InvestigationDate=?, Site=?" +
                ", River=?, Photo=?, StartTime=?, EndTime=?, StartLongitude=?, StartLatitude=?" +
                ", EndLongitude=?, EndLatitude=?, Weather=?, Temperature=? WHERE InverstigationID=?";
        System.out.println("更新操作");
        int num = getJdbcTemplate().update(sql, institution, invertigator, invertigationDate, site, river, photo, startTime
                , endTime, startLongitude, startLatitude, endLongitude, endLatitude, weather, temperature, invertigtaionID);
        System.out.println("更新的数量"+num);
        return num;
    }

}
